import {
  getSclNotCompleteInfo
} from '@/common/api/scl_api.js'
import { mapState } from 'vuex'

export default{
  data(){
    return {
      sclNotCompleteTipVisible:false,
      notCompleteSclNum:0,
      notCompleteSclInfo:{
        sclId:'',
        sclName:'',
      }
    }
  },
  computed:{
    ...mapState({
      initGroup:state=>state.user.initGroup,
    })
  },
  methods:{
    checkNotCompleteScl(sclId,fn){
      if(this.notCompleteSclInfo.sclName && (sclId != this.notCompleteSclInfo.sclId)){
        
        let qstnCode 
        if(this.notCompleteSclInfo.detailInfo){
          let tmpObj
          //检查是否有跳转规则
          try {
            tmpObj = JSON.parse(this.notCompleteSclInfo.detailInfo)
          } catch (e) {
            //TODO handle the exception
            tmpObj = {}
          }
          if(tmpObj.introType=='32-multi-mbti'){
            qstnCode = 100
          }
        }
        
        uni.showModal({
            title:`《${this.notCompleteSclInfo.sclName}》并未完成确认后继续测试`,
            cancelText:"继续",
            confirmText:"放弃",
            cancelColor:'#666666',
            confirmColor:'#666666',
            success:(res)=>{
              if(res.cancel){
                uni.navigateTo({
                  url: '/subcontractorD/pages/sclDoQuestion/sclDoQuestion?paramObj=' + JSON.stringify({
                    sclId:this.notCompleteSclInfo.sclId,
                    from:'unComplete',
                    qstnCode
                  })
                })
              }else if(res.confirm){
                fn()  
              }
            },
            fail:()=>{
              console.log('checkNotCompleteScl fail')
            }
          })
        
      }else{
        fn()  
      }
    },
    async checkRedirect(sclId){
      const res = await this.$u.api.getSclInfo({sclId})
      res.data.data = res.data.data || {}
      const {
        scloReportDetail,
        detailInfo
      } = res.data.data || {}
      
      const tmpObj = JSON.parse(detailInfo) || {}
      
      console.log(tmpObj,'---tmpObj.redirect')
      
      if (tmpObj.redirect == 'checkup-list') {
        uni.redirectTo({
          url: '/subcontractorC/pages/sclCheckup/sclCheckup?sourceType=11&from=qy&sclId='+sclId+'&qyType='+tmpObj.qyRedirectType
        })
        return true
      }
    },
    async confirmSclNotCompleteTipComp(){
      console.log(this.notCompleteSclInfo,'----notCompleteSclInfo')
      
      this.$u.api.addLog({sclId:this.notCompleteSclInfo.sclId,sourseType:11})
      // if(!this.initGroup){
      //     uni.navigateTo({
      //     	url: '/subcontractorB/pages/initialAppraisal/initialAppraisal?sclId=' + this.notCompleteSclInfo.sclId
      //     })
      // }else{
        
        if(await this.checkRedirect(this.notCompleteSclInfo.sclId)){
          return false
        }
        
        uni.navigateTo({
          url: '/subcontractorC/pages/sclIntroduce/sclIntroduce?paramObj=' + JSON.stringify({
            sclId:this.notCompleteSclInfo.sclId,
            from:'unComplete',
            equityType: 'equityType' //随便填点数据只要不为空就行，
          })
        })
      // }
    },
    hideSclNotCompleteTipComp(){
      this.sclNotCompleteTipVisible = false
    },
    async getSclNotCompleteInfo(){
      const {data:dRes} = await getSclNotCompleteInfo()
      if(dRes.success){
        
        if(dRes.data){
          console.log(dRes.data,'=========dRes.datadRes.datadRes.datadRes.data')  
          this.notCompleteSclNum = dRes.data.length
          if(this.notCompleteSclNum>0){
            this.sclNotCompleteTipVisible = true  
            this.notCompleteSclInfo = dRes.data[0]
          }else{
            this.sclNotCompleteTipVisible = false
            this.notCompleteSclInfo = {
              sclId:'',
              sclName:'',
            }  
          }
        }else{
          this.sclNotCompleteTipVisible = false
          this.notCompleteSclInfo = {
            sclId:'',
            sclName:'',
          }
        }
        
      }
      console.log('----getSclNotCompleteInfo====',dRes)
    },
    
  }
}